/*
* netproc.c: server procedures for p2
*/
#include <iostream>
#include "netmgt.h"

#define MAXLENGTH 1024000
#define SMALLBUFFER 1024

//char buffer[MAXLENGTH];

char** runcmd_1_svc(char **cmd, struct svc_req *req)
{
	std::cout << "Accepted client.\n";

	static char* ptr;
	static char buffer[MAXLENGTH];
	char readBuffer[SMALLBUFFER];

	std::cout << "Executing " << *cmd << ".\n";

	FILE* fp = popen(*cmd, "r");

        if (fp != NULL)
        {
            memset(buffer, '\0', MAXLENGTH);
	    memset(readBuffer, '\0', SMALLBUFFER);
            while (fgets(readBuffer, SMALLBUFFER - 1, fp))
            {
		strcat(buffer, readBuffer);
                memset (readBuffer, '\0', SMALLBUFFER);
            }
            fclose(fp);
        }
        ptr = buffer;
        std::cout << "Execution complete.\n";
	return &ptr;
}
